package cn.bgw;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class ListRemoveTest {

    public static void main(String[] args) throws ClassNotFoundException {
        // removeTestt();
        // classTest();
        testDate();
    }

    public static void testDate() {
        String qq = "20170919";
        // 验证日期格式为YYYY-MM-DD的正则表达式为
        String regex = "((\\d{3}[1-9]|\\d{2}[1-9]\\d|\\d[1-9]\\d{2}|[1-9]\\d{3})(((0[13578]|1[02])(0[1-9]|[12]\\d|3[01]))|((0[469]|11)(0[1-9]|[12]\\d|30))|(02(0[1-9]|[1]\\d|2[0-8]))))|(((\\d{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229)";
        boolean flag = qq.matches(regex);
        if (flag)
            System.out.println(qq + "...is ok");
        else
            System.out.println(qq + "... 不合法");
    }

    private static void removeTestt() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(9);
        list.add(8);
        list.add(7);
        removeTest(list, 9);
        System.out.println(Arrays.toString(list.toArray()));
    }

    private static void classTest() throws ClassNotFoundException {
        /**
         * 获取class对象的三种方式；
         */
        Class<ListRemoveTest> clz1 = ListRemoveTest.class;
        ListRemoveTest instance = new ListRemoveTest();
        Class<? extends ListRemoveTest> clz2 = instance.getClass();
        Class<?> clz3 = Class.forName("cn.bgw.ListRemoveTest");
        System.out.println(clz1 == clz2);
        System.out.println(clz1 == clz3);
    }


    public static void removeTest(List<Integer> list, Integer target) {
        // remove1(list, target);
        // remove2(list, target);
        // remove3(list, target);
    }

    private static void remove3(List<Integer> list, Integer target) {
        int len = list.size();
        for (int i = len - 1; i >= 0; i--) {
            Integer item = list.get(i);
            if (item == target) {
                list.remove(item);
            }
        }
    }

    private static void remove2(List<Integer> list, Integer target) {
        final CopyOnWriteArrayList<Integer> arrayList = new CopyOnWriteArrayList(list);
        for (Integer i : arrayList) {
            if (i == target) {
                arrayList.remove(i);
            }
        }
    }

    private static void remove1(List<Integer> list, Integer target) {
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            if (next == target) {
                iterator.remove();
            }
        }
    }
}
